package com.erytech.linktalk.dao.ibatis;

import java.sql.Timestamp;
import java.util.List;

import com.erytech.linktalk.dao.MessageDao;
import com.erytech.linktalk.dao.domain.Message;
import com.erytech.linktalk.dao.domain.User;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;

public class SqlMapMessageDao extends SqlMapDaoTemplate implements MessageDao {

	public SqlMapMessageDao(DaoManager daoManager) {
		super(daoManager);
	}

	public List getMessageList(User user) {
		return queryForList("getMessageList", user);
	}

	public Message insertMessage(Message message) {
		insert("insertMessage", message);
		return message;
	}

	public void insertEnterRoomMessage(User user) {
		Message message = new Message();
		message.setBody(user.getName() + " 进入聊天室.");
		message.setFrom(user.getName());
		message.setPostTime(user.getCreationTime());
		message.setRoomId(user.getRoomId());
		message.setTo(User.ALL);
		
		insertMessage(message);
	}

	public void insertLeftRoomMessage(User user) {
		Message message = new Message();
		message.setBody(user.getName() + " 离开聊天室.");
		message.setFrom(user.getName());
		message.setPostTime(user.getLastPollTime());
		message.setRoomId(user.getRoomId());
		message.setTo(User.ALL);
		insertMessage(message);
	}

	public void deleteOldMessages(Timestamp timestamp) {
		delete("deleteOldMessages", timestamp);
	}

}
